/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vnetlab.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author ROGERIO
 */
public class Configuration {

    private static Configuration instance;

    private String filePath;
    private String vBoxManagePath;
    private String templatesPath;
    private String vmPath;
    private String configPath;
    private String routerTemplate;
    private String computerTemplate;
    private String serverTemplate;

    private Configuration(){
        routerTemplate = computerTemplate = serverTemplate = "Slitaz.ovf";
        configPath = ((new  File(System.getProperty("user.home"))).getAbsolutePath().replace(".", "")) +File.separator+"VNetLab.properties";
        System.out.println(configPath);
        load();
    }

    public static  Configuration getInstance(){
        if(instance == null)
            instance = new Configuration();

        return instance;
    }

    public String getConfigPath() {
        return configPath;
    }

    public String getVmPath() {
        return vmPath;
    }

    public void setVmPath(String vmPath) {
        this.vmPath = vmPath;
    }

    public void setvBoxManagePath(String vBoxManagePath) {
        this.vBoxManagePath = vBoxManagePath;
    }

    public void setTemplatesPath(String templatesPath) {
        this.templatesPath = templatesPath;
    }



    public String getVBoxManagePath() {
        return vBoxManagePath;
    }

    public String getTemplatesPath() {
        return templatesPath;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
        this.setVmPath(getVmPath(new File(filePath)));
    }

    public void load(){

        FileInputStream arquivoConfig = null;
        Properties configSistema = new Properties();

        try {
            File file = new File(configPath);
            if(!file.exists())
                file.createNewFile();
            arquivoConfig = new FileInputStream(configPath);
            configSistema.load(arquivoConfig);

            this.templatesPath = configSistema.getProperty("templates");
            this.vBoxManagePath = configSistema.getProperty("vboxmanage");

            this.routerTemplate = (configSistema.getProperty("routertemplate") != null ? configSistema.getProperty("routertemplate") : this.computerTemplate);
            this.computerTemplate = (configSistema.getProperty("computertemplate") != null  ?configSistema.getProperty("computertemplate")  : this.computerTemplate );
            this.serverTemplate = (configSistema.getProperty("servertemplate")!=null  ?configSistema.getProperty("computertemplate")  : this.serverTemplate);
            

        }
        catch(Exception e) {
            //e.printStackTrace();
        }
        finally {
            try {
                arquivoConfig.close();
            }
            catch(IOException e) {
                //e.printStackTrace();
            }
        }

    //    return configSistema;
    }

    public void store(){
        FileOutputStream arquivoConfig = null;
        Properties configSistema = new Properties();

        try {
            arquivoConfig = new FileOutputStream(configPath);

            configSistema.setProperty("templates",this.templatesPath);
            configSistema.setProperty("vboxmanage",this.vBoxManagePath);
            configSistema.setProperty("routertemplate", this.routerTemplate);
            configSistema.setProperty("computertemplate", this.computerTemplate);
            configSistema.setProperty("servertemplate", this.serverTemplate);

            configSistema.store(arquivoConfig, "");
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                arquivoConfig.close();
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getVmPath(File file){
        //Cria o diretorio onde ficaria as vms
        File vmDirectory = new File(file.getParentFile().getAbsolutePath()+ File.separator +file.getName().substring(0,file.getName().indexOf(".") )+"_VMs");
        vmDirectory.mkdirs();

        return vmDirectory.getAbsolutePath();
    }

    public void setComputerTemplate(String computerTemplate) {
        this.computerTemplate = computerTemplate;
    }

    public void setRouterTemplate(String routerTemplate) {
        this.routerTemplate = routerTemplate;
    }

    public void setServerTemplate(String serverTemplate) {
        this.serverTemplate = serverTemplate;
    }

    public String getComputerTemplate() {
        return computerTemplate;
    }

    public String getRouterTemplate() {
        return routerTemplate;
    }

    public String getServerTemplate() {
        return serverTemplate;
    }
}

